import datetime
import time
from django.http import JsonResponse
from django.shortcuts import render
from django.conf import settings
from stocksys.sys_tasks.logic_function import get_realtime_stock_content
from stocksys.views.user_views import check_login
from stocksys.views.views import init_message
from stocksys import models as stocksys_models
from django.contrib.auth import models as auth_models

@check_login
def conceptual_panel(request):
    context = init_message(request)
    context["title"] = "量化分析系统-概念板"
    conceptuals = stocksys_models.Concept.objects.all()
    context["conceptuals"] = conceptuals
    context["now_year"] = int(time.strftime('%Y', time.localtime(time.time())))
    context["quant_start_year"] = context["now_year"] - 1
    context["quant_rule"] = "base_linear_regression"
    context["spider_mode"] = False
    if settings.USE_JQDATA_SDK:
        context["spider_mode"] = True

    return render(request, "stocksys/table_list/concept_panel.html", context)


@check_login
def conceptual_info(request, code, addr, year=None):
    info = {"statue": 500, "info": "conceptual_info"}
    if request.method == "GET":
        return JsonResponse(info)
    elif request.method == "POST":
        return JsonResponse(info)


@check_login
def conceptual_time_info(request, code):
    # TODO:单支概念数据
    return JsonResponse({"statue": 200, "page": "conceptual_time_info"})

# TODO:获取概念成分股
@check_login
def concept_stock(request, id):
    if request.method == "GET":
        context = init_message(request)
        context["title"] = "概念成分股"
        concept = stocksys_models.Concept.objects.get(id=id)
        stock_concepts = stocksys_models.StockConcept.objects.filter(concept=concept)
        user = auth_models.User.objects.get(id=request.session.get('id'))
        sysconf = stocksys_models.SysConf.objects.get(user=user)

        stock_concept_list=[]
        for stock_concept in stock_concepts:
            exchange = stock_concept.stock
            stock_code = "%s%s" % (exchange.exchange_address.stock_sina_name, exchange.code)
            info = get_realtime_stock_content(stock_code)
            exchange_chg = 0
            if info['current'] != 0:
                exchange_chg = (info['current'] - info['close']) / info['close']
            if exchange_chg < 0:
                color = 'success'
            elif exchange_chg == 0:
                color = 'warning'
            else:
                color = 'danger'
            stock_concept_list.append({
                'name': exchange.name,
                'abbreviated_name': exchange.abbreviated_name,
                'exchange_address': exchange.exchange_address,
                'code': exchange.code,
                'type': exchange.type,
                'open': info['open'],
                'close': info['close'],
                'current': info['current'],
                'chg': exchange_chg * 100,
                'chg_price': exchange_chg,
                'color': color,
                'volume': info['v_num'] / 10000,
                'money': info['money'] / 10000,
                "systime": datetime.datetime.now().strftime('%Y-%m-%d'),
            })
        context["now_year"] = int(time.strftime('%Y', time.localtime(time.time())))
        context["stock_concepts"] = stock_concept_list
        context["now_year"] = int(time.strftime('%Y', time.localtime(time.time())))
        context["quant_start_date"] = sysconf.predict_start_time.strftime("%Y-%m-%d")
        context["quant_end_date"] = sysconf.predict_end_time.strftime("%Y-%m-%d")
        context["predict_type"] = 'stock'
        context["data_method"] = 'sina'
        if settings.USE_JQDATA_SDK:
            context["data_method"] = 'jqdata'
        return render(request, "stocksys/table_list/concept_stock.html", context)
